/*
 * Copyright (C) 1994-2021 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of both the OpenPBS software ("OpenPBS")
 * and the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * OpenPBS is free software. You can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * OpenPBS is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * PBS Pro is commercially licensed software that shares a common core with
 * the OpenPBS software.  For a copy of the commercial license terms and
 * conditions, go to: (http://www.pbspro.com/agreement.html) or contact the
 * Altair Legal Department.
 *
 * Altair's dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of OpenPBS and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair's trademarks, including but not limited to "PBS™",
 * "OpenPBS®", "PBS Professional®", and "PBS Pro™" and Altair's logos is
 * subject to Altair's trademark licensing policies.
 */

#include <pbs_config.h> /* the master config generated by configure */
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/wait.h>
#include <dirent.h>
#include "tpp.h"
#include "pbs_ifl.h"
#include "list_link.h"
#include "attribute.h"
#include "job.h"
#include "ticket.h"
#include "libpbs.h"
#include "batch_request.h"
#include "pbs_nodes.h"
#include "mom_func.h"

/**
 * @file	stage_func.c
 */
extern char *path_spool;	/* path to spool directory */
extern char *path_undeliv;	/* path to undelivered directory */
extern char *path_checkpoint;	/* path to checkpoint directory */
extern char *msg_err_unlink;	/* unlink error message (see pbs_messages.c) */
extern char rcperr[MAXPATHLEN]; /* path to rcp errfile for current copy */
extern char *pbs_jobdir;	/* path to staging and execution dir of current job */
extern char *cred_buf;		/* cred buffer */
extern size_t cred_len;		/* length of cred buffer */
#ifndef WIN32
extern int cred_pipe;
extern char *pwd_buf;
#endif
extern char mom_host[PBS_MAXHOSTNAME + 1]; /* MoM host name */

int stage_file(int, int, char *, struct rqfpair *, int, cpy_files *, char *, char *);
static int sys_copy(int, int, char *, char *, struct rqfpair *, int, char *, char *);

/**
 * A path in windows is not case sensitive so do a define
 * to do the right compare.
 */
#ifdef WIN32
#define PATHCMP strncasecmp
#else
#define PATHCMP strncmp
#endif

#ifdef WIN32
/**
 * @brief
 * check_err - Report the error in log file, if the length of actual data
 *             buffer and length of the total data written is different.
 *
 * @param[in]		func_name		-	Name of the caller function
 * @param[in]		buffer			-	Actual data buffer
 * @param[in]		written_len		-	Total length of the written data
 *
 * @return void
 */
void
check_err(const char *func_name, char *buffer, int written_len)
{
	if (written_len != strlen(buffer)) {
		DWORD ecode = GetLastError();
		snprintf(log_buffer, sizeof(log_buffer),
			 "Failed to write or written partial data to the pipe: data=[%s], total_len=%d, written_len=%d, ecode=%d",
			 buffer, strlen(buffer), written_len, ecode);
		log_err(-1, func_name, log_buffer);
	}
}
#endif

/**
 * @brief
 *	add_bad_list
 *	add new bad file message to bad file messages list
 *
 * @param[in/out]	pbl	-	list of bad file messages
 * @param[in]		newtext	-	new bad file message
 * @param[in]		nl	-	number of prefix new-lines
 *
 * @return	void
 *
 * @note: if <pbl> is non-NULL then it will be reallocated based on length of
 *        <newtext> or if <pbl> is NULL then it will be allocated
 *        So, caller should free <pbl> after use
 *
 */
void
add_bad_list(char **pbl, char *newtext, int nl)
{
	int needed = 0;
	char *pnew = NULL;

	if (*pbl) {
		needed += strlen(*pbl) + strlen(newtext) + nl + 1;
		pnew = realloc(*pbl, needed);
	} else {
		needed += strlen(newtext) + nl + 1;
		pnew = malloc(needed);
		if (pnew)
			*pnew = '\0';
	}
	if (pnew == NULL) {
		log_err(errno, __func__, "Failed to allocate memory");
		return;
	}

	*pbl = pnew;
	while (nl--) /* prefix new-lines */
		(void) strcat(*pbl, "\n");
	(void) strcat(*pbl, newtext);
	return;
}

/**
 * @brief
 *	is_child_path
 *		check if provided <path> specifies location under specified directory <dir>
 *
 * @param[in]	dir	-	directory path
 * @param[in]	path	-	path to check
 *
 * @return	int
 * @retval	1 - given path is child of dir
 * @retval	0 - given not a child path
 * @retval -1 - error encountered
 */
int
is_child_path(char *dir, char *path)
{
	char fullpath[2 * MAXPATHLEN + 2] = {'\0'};
	char *dir_real = NULL;
	char *fullpath_real = NULL;
	char *pos = NULL;
	int return_value = 0;

	/* if file path is relative, combine it with directory */
	if (!is_full_path(path)) {
		snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, path);
	} else {
		snprintf(fullpath, sizeof(fullpath), "%s", path);
	}

#ifdef WIN32
	dir_real = malloc(sizeof(char) * (MAXPATHLEN + 1));
	fullpath_real = malloc(sizeof(char) * (2 * MAXPATHLEN + 2));
#else
	dir_real = realpath(dir, NULL);
	fullpath_real = realpath(fullpath, NULL);
#endif

	if (dir_real == NULL || fullpath_real == NULL) {
		log_err(errno, __func__, "Failed to allocate memory");
		return_value = -1;
		goto error_exit;
	}

	/* even if the file path is relative to some directory, */
	/* it may use /../../ notation and escape that directory, */
	/* so always perform full check of parent-child directory relation */
#ifdef WIN32
	fix_path(fullpath, 3);
	pbs_strncpy(dir_real, lpath2short(dir), MAXPATHLEN + 1);
	pbs_strncpy(fullpath_real, lpath2short(fullpath), 2 * MAXPATHLEN + 2);
#endif
	/* check that fullpath_real begins with dir_real */
	if (strlen(dir_real) && strlen(fullpath_real)) {
		pos = strstr(fullpath_real, dir_real);
		if (pos == fullpath_real) {
			return_value = 1;
		}
	}

error_exit:
	free(dir_real);
	free(fullpath_real);
	return return_value;
}

/**
 * @brief	wchost_match
 *		Wild card host name match.
 *		Do a case insensitive compare since hostnames are not case sensitive.
 *
 * @param[in]	can	-	stage request hostname
 * @param[in]	master	-	name from usecp list (may be wild carded at beginning)
 *
 * @return	int
 * @retval	1 - can"idate" matches master name
 * @retval	0 - not a match
 *
 */
int
wchost_match(const char *can, const char *master)
{
	const char *pc;
	const char *pm;

	if ((can == NULL) || (master == NULL))
		return 0;

	pc = can + strlen(can) - 1;
	pm = master + strlen(master) - 1;
	while ((pc > can) && (pm > master)) {
		if (tolower(*pc) != tolower(*pm))
			return 0;
		pc--;
		pm--;
	}

	/* comparison of one or both reached the start of the string */
	if (pm == master) {
		if (*pm == '*')
			return 1;
		else if ((pc == can) && (tolower(*pc) == tolower(*pm)))
			return 1;
	}
	return 0;
}

#ifdef NAS /* localmod 009 */
/**
 * @brief
 *	Wild card suffix host name match.  Do a case insensitive compare since
 * 	hostnames are not case sensitive.
 *
 * @param can           stage request hostname
 * @param master        name from usecp list (may be wild carded at end)
 *
 * @return int
 * @retval 1    candidate matches master name
 * @retval 0    not a match
 *
 */
static int
	wcsuffix_host_match(can, master)
		const char *can;
const char *master;
{
	int cmp_length;
	int master_length = strlen(master);

	if (master[master_length - 1] == '*') {
		cmp_length = master_length - 1;
	} else {
		cmp_length = master_length;
	}

	if (strncasecmp(can, master, cmp_length) == 0)
		return 1;

	return 0;
}
#endif /* localmod 009 */

/**
 * @brief
 *	told_to_cp - Check a stage file request against the saved set of "usecp" paths.
 *
 * @param[in]	host	-	stage request hostname
 * @param[in]	oldpath	-	stage request path
 * @param[out]	newpath	-	pointer to "usecp" path
 *
 * @return	int
 * @retval	1 - file matched, newpath is updated
 * @retval	0 - no match, newpath is unchanged
 *
 */
int
told_to_cp(char *host, char *oldpath, char **newpath)
{
	int i = 0;
	int nh = 0;
	static char newp[MAXPATHLEN + 1] = {'\0'};

#ifdef NAS /* localmod 009 */
	extern struct cphosts *pcphosts;
	int match_found = 0;
	for (nh = 0; nh < cphosts_num; nh++) {
		if (wchost_match(host, (pcphosts + nh)->cph_hosts) ||
		    wcsuffix_host_match(host, (pcphosts + nh)->cph_hosts)) {
			i = strlen((pcphosts + nh)->cph_from);
			if (PATHCMP((pcphosts + nh)->cph_from, oldpath, i) == 0) {
				if ((pcphosts + nh)->cph_exclude)
					return 0;

				match_found = 1;
				pbs_strncpy(newp, (pcphosts + nh)->cph_to, sizeof(newp));
				(void) strcat(newp, oldpath + i);
			}
		}
	}

	if (match_found) {
		*newpath = newp;
	}

	return match_found;
#else
	for (nh = 0; nh < cphosts_num; nh++) {
		if (wchost_match(host, (pcphosts + nh)->cph_hosts)) {
			i = strlen((pcphosts + nh)->cph_from);
			if (PATHCMP((pcphosts + nh)->cph_from, oldpath, i) == 0) {
				(void) strcpy(newp, (pcphosts + nh)->cph_to);
				(void) strcat(newp, oldpath + i);
				*newpath = newp;
				return 1;
			}
		}
	}
	return 0;
#endif /* localmod 009 */
}

/**
 * @brief
 *	local_or_remote - Decide if the specified path is to a local or remote file.
 *
 * @param[in]	path	-	pointer to stage path string
 *
 * @return	int
 * @retval	1 - if given path is remote path
 * @retval	0 - if given path is local path
 *
 * @note	This function will updates the path pointer to just the path name if local.
 *
 */
int
local_or_remote(char **path)
{
	int len = 0;
	char *pcolon = NULL;

	pcolon = strchr(*path, (int) ':');
	if (pcolon == NULL)
		return 0;

	*pcolon = '\0';
	len = strlen(*path);
#ifdef WIN32
	if (IS_UNCPATH(pcolon + 1)) {
		/*
		 * UNC path found
		 * treat as local path and
		 * remove given hostname part from path
		 */
		*pcolon = ':';
		*path = pcolon + 1;
		return 0;
	} else if (told_to_cp(*path, pcolon + 1, path))
#else
	if (told_to_cp(*path, pcolon + 1, path))
#endif
	{
		/* path updated in told_to_cp() */
		*pcolon = ':';
		return 0;
	} else if ((strcasecmp("localhost", *path) == 0) ||
		   ((strncasecmp(mom_host, *path, len) == 0) &&
		    ((mom_host[len] == '\0') || (mom_host[len] == '.')))) {
		/* we have a host match, file is local */
		*pcolon = ':';
		*path = pcolon + 1;
		return 0;
	} else {
		/* remote file */
		*pcolon = ':';
		return 1;
	}
}

/**
 * @brief
 *	Setup for direct write of spool file
 * @par
 *	Determines if a spool file is to be directly written to its final destination, i.e:
 *	1. Direct write of spool files has been requested by the job, and
 *  2. Final destination of the file maps to a locally-mounted directory, either because it is
 *	   explicitly mapped by $usecp, or the destination hostname is Mom's host.
 *
 * @param[in]  pjob - pointer to job structure
 * @param[in]  which - identifies which file: StdOut, StdErr, or Chkpt.
 * @param[out]  path -  pointer to array of size MAPATHXLEN+1 into which the final path of the file is to be stored.
 * @param[out]  direct_write_possible -  Determines whether direct_write is possible.
 *                                         Useful to decide whether to write warning message to stderr file
 *                                          after multiple function invocation.
 *
 * @return int
 *
 * @retval	0 if file is not to be directly written,
 * @retval	1 otherwise.
 *
 * @par
 *	If the file is not to be directly written (return zero), the contents of *path are unchanged.
 * @par
 *	Direct write of checkpoint files is not currently supported.
 *
 * @par @par MT-safe: No
 *
 */
int
is_direct_write(job *pjob, enum job_file which, char *path, int *direct_write_possible)
{
	char working_path[MAXPATHLEN + PBS_MAXSVRJOBID + 3 + 1];
	char *p = working_path;

	if (which == Chkpt)
		return (0); /* direct write of checkpoint not supported */

	/* Check if direct_write requested. */
	if (!((is_jattr_set(pjob, JOB_ATR_keep)) &&
	      (strchr(get_jattr_str(pjob, JOB_ATR_keep), 'd'))))
		return (0);

	/* Figure out what the final destination path is */
	switch (which) {
		case StdOut:
			if (!strchr(get_jattr_str(pjob, JOB_ATR_keep), 'o'))
				return (0);
			else
				/* Make local working copy of path for call to local_or_remote */
				snprintf(working_path, MAXPATHLEN + 1, "%s", get_jattr_str(pjob, JOB_ATR_outpath));
			if (
#ifdef WIN32
				working_path[strlen(working_path) - 1] == '\\'
#else
				working_path[strlen(working_path) - 1] == '/'
#endif
			) {
				strcat(working_path, pjob->ji_qs.ji_jobid);
				strcat(working_path, JOB_STDOUT_SUFFIX);
			}
			break;
		case StdErr:
			if (!strchr(get_jattr_str(pjob, JOB_ATR_keep), 'e'))
				return (0);
			else
				/* Make local working copy of path for call to local_or_remote */
				snprintf(working_path, MAXPATHLEN + 1, "%s", get_jattr_str(pjob, JOB_ATR_errpath));
			if (
#ifdef WIN32
				working_path[strlen(working_path) - 1] == '\\'
#else
				working_path[strlen(working_path) - 1] == '/'
#endif
			) {
				strcat(working_path, pjob->ji_qs.ji_jobid);
				strcat(working_path, JOB_STDERR_SUFFIX);
			}
			break;
		default:
			return (0);
	}

	if (local_or_remote(&p) == 1) {
		*direct_write_possible = 0;
		if (pjob->ji_hosts != NULL) {
			log_eventf(PBSEVENT_DEBUG3,
				   PBS_EVENTCLASS_JOB, LOG_DEBUG, pjob->ji_qs.ji_jobid,
				   "Direct write is requested for job: %s, but the destination: %s is not usecp-able from %s",
				   pjob->ji_qs.ji_jobid, p,
				   pjob->ji_hosts[pjob->ji_nodeid].hn_host);
		} else {
			/* When a job is requeued and later run, the ji_hosts
			 * information is not available when this function is
			 * called as part of req_mvjobfile
			 */
			log_eventf(PBSEVENT_DEBUG3,
				   PBS_EVENTCLASS_JOB, LOG_DEBUG, pjob->ji_qs.ji_jobid,
				   "Direct write is requested for job: %s, but the destination: %s is not usecp-able",
				   pjob->ji_qs.ji_jobid, p);
		}
		return (0);
	}

	if (strlen(p) > MAXPATHLEN) {
		*direct_write_possible = 0;
		sprintf(log_buffer,
			"Direct write is requested for job: %s, but the destination path is longer than %d",
			pjob->ji_qs.ji_jobid, MAXPATHLEN);
		log_event(PBSEVENT_DEBUG3,
			  PBS_EVENTCLASS_JOB, LOG_DEBUG, pjob->ji_qs.ji_jobid, log_buffer);
		return (0);
	}

	/* Destination maps to local directory - final path is in working_path. */
	snprintf(path, MAXPATHLEN + 1, "%s", p);
	return (1);
}

#ifdef WIN32
/**
 * @brief
 *	remtree - wrapper function to remove a tree (or single file) to support for UNC path
 *
 * @param[in]	dirname	-	path of single file or dir to remove
 *
 * @return	int
 * @retval	0 - success
 * @retval	-1 - failure
 *
 */
int
remtree(char *dirname)
{
	int rtnv = 0;
	char unipath[MAXPATHLEN + 1] = {'\0'};
	int unmap = 0;
	char map_drive[MAXPATHLEN + 1] = {'\0'};

	if (dirname != NULL && *dirname != '\0') {
		replace(dirname, "\\ ", " ", unipath);
		fix_path(unipath, 3);
	} else {
		log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, "directory or file path is NULL");
		return -1;
	}

	unmap = get_localpath(unipath, map_drive);

	rtnv = remdir(unipath);

	if (unmap)
		unmap_unc_path(map_drive);

	return rtnv;
}

/**
 * @brief
 *	remove a tree (or single file)
 *
 * @param[in]	path - path of single file or dir to remove
 *
 * @return 	int
 * @retval 	0 on success
 * @retval	-1 on failure
 *
 */
int
remdir(char *dirname)
#else /* WIN32 */
int
remtree(char *dirname)
#endif
{
#ifdef WIN32
	static char id[] = "remdir";
#else
	static char id[] = "remtree";
#endif
	DIR *dir = NULL;
	struct dirent *pdir = NULL;
	char namebuf[MAXPATHLEN] = {'\0'};
	char *filnam = NULL;
	int i = 0;
	int rtnv = 0;
	struct stat sb = {0};

	if (lstat(dirname, &sb) == -1) {
		if (errno != ENOENT) {
			sprintf(log_buffer, "lstat: %s", dirname);
			log_err(errno, id, log_buffer);
		}
		return -1;
	}

	if (S_ISDIR(sb.st_mode)) {
		if ((dir = opendir(dirname)) == NULL) {
			if (errno != ENOENT) {
				sprintf(log_buffer, "opendir: %s", dirname);
				log_err(errno, id, log_buffer);
			}
			return -1;
		}

		pbs_strncpy(namebuf, dirname, sizeof(namebuf));
#ifdef WIN32
		(void) strcat(namebuf, "\\");
#else
		(void) strcat(namebuf, "/");
#endif
		i = strlen(namebuf);
		filnam = &namebuf[i];

		while (errno = 0, (pdir = readdir(dir)) != NULL) {
			if (pdir->d_name[0] == '.') {
				if (pdir->d_name[1] == '\0' ||
				    (pdir->d_name[1] == '.' &&
				     pdir->d_name[2] == '\0'))
					continue;
			}

			pbs_strncpy(filnam, pdir->d_name, sizeof(namebuf) - (filnam - namebuf));
#ifdef WIN32
			rtnv = remdir(namebuf);
#else
			rtnv = remtree(namebuf);
#endif
			if (rtnv == -1) {
				errno = 0;
				break;
			}
		}
		if (errno != 0 && errno != ENOENT) {
			sprintf(log_buffer, "readdir: %s", dirname);
			log_err(errno, id, log_buffer);
			(void) closedir(dir);
			return -1;
		}
		(void) closedir(dir);

		if (rmdir(dirname) < 0) {
			if (errno != ENOENT) {
				sprintf(log_buffer, "rmdir: %s", dirname);
				log_err(errno, id, log_buffer);
				rtnv = -1;
			}
		}
	} else if (unlink(dirname) < 0) {
		sprintf(log_buffer, "unlink: %s", dirname);
		log_err(errno, id, log_buffer);
		rtnv = -1;
	}
	return rtnv;
}

/**
 * @brief
 *	pbs_glob - check whether given pattern matches in filename
 *	pattern matches:
 *	*	matches zero or more characters
 *	?	matches any single character
 *	char	matches itself except where char is '*' or '?'
 *
 * @param[in]	filen	-	filename
 * @param[in]	pat	-	pattern to match
 *
 * @return	Boolean
 * @retval	TRUE - if pattern matches
 * @retval	FALSE - if no match
 *
 */
int
pbs_glob(char *filen, char *pat)
{
	int c = 0;

	while (*pat) {
		if (!*filen && *pat != '*')
			return FALSE;

		c = *pat++;
		switch (c) {

			case '*':
				while (*pat == '*')
					pat++;

				if (!*pat)
					return TRUE;

				if (*pat != '?') {
					while (*filen && *pat != *filen)
						filen++;
				}

				while (*filen) {
					if (pbs_glob(filen, pat))
						return TRUE;
					filen++;
				}
				return FALSE;

			case '?':
				if (*filen)
					break;
				return FALSE;

			default:
				if (c != *filen)
					return FALSE;
				break;
		}
		filen++;
	}

	return !*filen;
}

/**
 * @brief
 *	copy_file - Do a single staging file copy.
 *
 * @param[in]		dir		-	direction of copy
 *						STAGE_DIR_IN - for stage in request
 *						STAGE_DIR_OUT - for stageout request
 * @param[in]		rmtflag		-	is remote file copy
 * @param[in]		owner		-	username for owner of copy request
 * @param[in]		src		-	path to source is stageout else local file name
 * @param[in]		pair		-	list of file pair
 * @param[in]		conn		-	socket on which request is received
 * @param[in/out]	stage_inout	-	pointer to cpy_files struct
 * @param[in]		prmt		-	path to destination if stageout else source path
 * @param[in]		jobid		- 	job ID
 *
 * @return	int
 * @retval	0 - all OK
 * @retval	!0 - error
 *
 */
int
copy_file(int dir, int rmtflag, char *owner, char *src, struct rqfpair *pair, int conn, cpy_files *stage_inout, char *prmt, char *jobid)
{
	int rc = 0;
	int ret = 0;
	int len = 0;
	struct stat buf = {0};
	char dest[MAXPATHLEN + 1] = {'\0'};
	char src_file[MAXPATHLEN + 1] = {'\0'};

	/*
	 ** The destination is calcluated for a stagein so it can
	 ** be used later.  It does not need to be passed to sys_copy.
	 */
	if (dir == STAGE_DIR_IN) {
		/* if destination is a directory, append filename */
#ifdef WIN32
		if (stat_uncpath(pair->fp_local, &buf) == 0 && S_ISDIR(buf.st_mode))
#else
		if (stat(pair->fp_local, &buf) == 0 && S_ISDIR(buf.st_mode))
#endif
		{
			char *slash = strrchr(src, '/');

			pbs_strncpy(dest, pair->fp_local, sizeof(dest));
			strcat(dest, "/");
			strcat(dest, (slash != NULL) ? slash + 1 : src);
		} else
			pbs_strncpy(dest, pair->fp_local, sizeof(dest));
	}

	ret = sys_copy(dir, rmtflag, owner, src, pair, conn, prmt, jobid);

	if (ret == 0) {
		/*
		 ** Copy worked.  If old behavior is used, a stageout file
		 ** is deleted now.  New behavior of waiting to delete
		 ** everything could be achived by adding the file to
		 ** a list to delete later.
		 */
		if (dir == STAGE_DIR_OUT) {
			/*
			 ** have copied out, may need to remove local file
			 ** if sandbox=private then the file is not removed here
			 ** it will be removed when the sandbox directory is removed
			 */

			if (!(stage_inout->sandbox_private &&
			      is_child_path(pbs_jobdir, src) == 1)) {
				/* Check if local file path has comma in it, if
				 * found escape character prefixed will be
				 * removed
				 */
				replace(src, "\\,", ",", src_file);
				if (*src_file == '\0')
					pbs_strncpy(src_file, src, sizeof(src_file));

				if (remtree(src_file) < 0) {
					if (errno == ENOENT) {
						log_event(PBSEVENT_ADMIN,
							  PBS_EVENTCLASS_FILE,
							  LOG_INFO, src,
							  "previously removed");
					} else {
						char temp[80 + MAXPATHLEN];

						snprintf(temp, sizeof(temp),
							 msg_err_unlink,
							 "stage out", src);
						log_err(errno, "req_cpyfile",
							temp);
						add_bad_list(&(stage_inout->bad_list),
							     temp, 2);
						stage_inout->bad_files = 1;
					}
				}
			}
		} else {
			/*
			 ** Add destination (local) filename to list so it can
			 ** be deleted on later failure.
			 */
			char temp[80 + MAXPATHLEN];
			char **stage_file_list_temp = NULL;
			if (stage_inout->file_max == stage_inout->file_num) { /* need to extend list */
				stage_inout->file_max += 10;
				if ((stage_file_list_temp = (char **) realloc(stage_inout->file_list, stage_inout->file_max * sizeof(char **))) == NULL) {
					snprintf(temp, sizeof(temp), "Out of Memory!");
					log_err(ENOMEM, "req_cpyfile", temp);
					return -1;
				} else {
					stage_inout->file_list = stage_file_list_temp;
				}
			}

			DBPRT(("%s: listadd %s\n", __func__, dest))
			if ((stage_inout->file_list[stage_inout->file_num++] = strdup(dest)) == NULL) {
				snprintf(temp, sizeof(temp), "Out of Memory!");
				log_err(ENOMEM, "req_cpyfile", temp);
				return -1;
			}
		}
	} else { /* failure */

		FILE *fp = NULL;
		DBPRT(("%s: sys_copy failed, error = %d\n", __func__, ret))
		snprintf(log_buffer, sizeof(log_buffer), "Job %s: sys_copy failed, return value=%d", jobid, ret);
		log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, log_buffer);
		stage_inout->bad_files = 1;
		snprintf(log_buffer, sizeof(log_buffer), "Unable to copy file %s %s %s",
			 (dir == STAGE_DIR_IN) ? dest : src,
			 (dir == STAGE_DIR_IN) ? "from" : "to",
			 (dir == STAGE_DIR_IN) ? src : pair->fp_rmt);
		add_bad_list(&(stage_inout->bad_list), log_buffer, 2);
		log_event(PBSEVENT_ADMIN, PBS_EVENTCLASS_FILE, LOG_INFO,
			  pair->fp_local, log_buffer);

		/* copy message from rcp as well */
		if ((fp = fopen(rcperr, "r")) != NULL) {
			add_bad_list(&(stage_inout->bad_list), ">>> error from copy", 1);
			while (fgets(log_buffer, LOG_BUF_SIZE, fp) != NULL) {
				len = strlen(log_buffer) - 1;

				/* strip up to 2 line endings */
				if (len >= 0) {
					if (log_buffer[len] == '\n' ||
					    log_buffer[len] == '\r')
						log_buffer[len] = '\0';
					len--;
					if (len >= 0) {
						if (log_buffer[len] == '\n' ||
						    log_buffer[len] == '\r')
							log_buffer[len] = '\0';
					}
				}
				add_bad_list(&(stage_inout->bad_list), log_buffer, 1);
				log_event(PBSEVENT_ADMIN, PBS_EVENTCLASS_FILE,
					  LOG_INFO, pair->fp_local, log_buffer);
			}
			fclose(fp);
			add_bad_list(&(stage_inout->bad_list), ">>> end error output", 1);
		} else {
			log_errf(errno, __func__, "Failed to open %s file", rcperr);
		}

		rc = -1;
		if (dir == STAGE_DIR_OUT) {
#ifndef NO_SPOOL_OUTPUT
			if (stage_inout->from_spool == 1) { /* copy out of spool */
				char undelname[MAXPATHLEN + 1];

				len = strlen(path_spool);
				pbs_strncpy(undelname, path_undeliv, sizeof(undelname));
				strcat(undelname, src + len); /* src path begins with spool */

				if (rename(src, undelname) == 0) { /* move file to undelivered */
					add_bad_list(&(stage_inout->bad_list), "Output retained on that host in: ", 1);
					add_bad_list(&(stage_inout->bad_list), undelname, 0);
				} else {
					char temp[80 + 2 * MAXPATHLEN];

					sprintf(temp, "Unable to rename %s to %s",
						src, undelname);
					log_err(errno, "req_cpyfile", temp);
				}
			}
#endif /* NO_SPOOL_OUTPUT */

			if (is_child_path(pbs_jobdir, src) == 1)
				stage_inout->stageout_failed = TRUE;
		}
	}

	unlink(rcperr);
	return rc;
}

/**
 * @brief
 *	stage_file - Handle file stage pair. The source could have a wildcard
 *	which would result in multipile copies.
 *
 * @param[in]		dir		-	direction of copy
 *							STAGE_DIR_IN - for stage in request
 *							STAGE_DIR_OUT - for stageout request
 * @param[in]		rmtflag		-	is remote file copy
 * @param[in]		owner		-	username for owner of copy request
 * @param[in]		pair		-	list of file pair
 * @param[in]		conn		-	socket on which request is received
 * @param[in/out]	stage_inout	-	pointer cpy_files struct
 * @param[in]		prmt		-	path to destination if stageout else source path
 * @param[in]		jobid		- 	job ID
 *
 * @return	int
 * @retval	0 - all OK
 * @retval 	!0 - error
 *
 */
int
stage_file(int dir, int rmtflag, char *owner, struct rqfpair *pair, int conn, cpy_files *stage_inout, char *prmt, char *jobid)
{
	char *ps = NULL;
	int i = 0;
	int rc = 0;
	int len = 0;
	char dname[MAXPATHLEN + 1] = {'\0'};
	char source[MAXPATHLEN + 1] = {'\0'};
	char matched[MAXPATHLEN + 1] = {'\0'};
	DIR *dirp = NULL;
	struct dirent *pdirent = NULL;
	struct stat statbuf;

	DBPRT(("%s: entered local %s remote %s\n", __func__, pair->fp_local, prmt))

	/*
	 * figure out the source path
	 */
	if (dir == STAGE_DIR_OUT) {
		source[0] = '\0';
		if (pair->fp_flag == STDJOBFILE) {
#ifndef NO_SPOOL_OUTPUT
			/* stdout | stderr from MOM's spool area */

			if (!(stage_inout->sandbox_private)) {
				DBPRT(("%s: STDJOBFILE from %s\n", __func__, path_spool))
				pbs_strncpy(source, path_spool, sizeof(source));
				stage_inout->from_spool = 1; /* flag as being in spool dir */
			}

			/*
			 * note, if NO_SPOOL_OUTPUT is defined, the
			 * output is in the user's home directory or job directory where
			 * we currently are.
			 */
#endif /* NO_SPOOL_OUTPUT */

		} else if (pair->fp_flag == JOBCKPFILE) {
			DBPRT(("%s: JOBCKPFILE from %s\n", __func__, path_checkpoint))
			pbs_strncpy(source, path_checkpoint, sizeof(source));
		}
		strcat(source, pair->fp_local);

		/* Staging out. Check to see if file is being staged out from spool directory (i.e., is stdout or stderr). If so,
		 * skip file if it doesn't exist in the spool directory, since it may have been directly written.
		 */

		if (stage_inout->from_spool && stage_inout->direct_write && !rmtflag) {
			if (stat(source, &statbuf) == -1) {
				if (errno == ENOENT) {
					sprintf(log_buffer,
						"Skipping directly written/absent spool file %s",
						source);
					log_event(PBSEVENT_DEBUG4, PBS_EVENTCLASS_JOB,
						  LOG_DEBUG, __func__, log_buffer);
					return 0;
				}
			}
		}

	} else { /* in bound (stage-in) file */
		/* take (remote) source name from request */
		pbs_strncpy(source, prmt, sizeof(source));
	}
	DBPRT(("%s: source %s\n", __func__, source))

	ps = strrchr(source, (int) '/');
	if (ps) {
		/* has prefix path, save parent directory name */
		len = (int) (ps - source) + 1;
		pbs_strncpy(dname, source, len + 1);
		ps++;
	} else {
		/* no prefix path, go with "./source" */
		dname[0] = '.';
		dname[1] = '/';
		dname[2] = '\0';
		ps = source;
	}

	if ((rmtflag != 0) && (dir == STAGE_DIR_IN)) { /* no need to check for wildcards */
		DBPRT(("%s: simple copy, remote/stagein\n", __func__))
		rc = copy_file(dir, rmtflag, owner, source,
			       pair, conn, stage_inout, prmt, jobid);
		if (rc != 0) {
			snprintf(log_buffer, sizeof(log_buffer), "Job %s: remote stagein failed for %s from %s to %s",
				 jobid, owner, source, pair->fp_local);
			log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, log_buffer);
			goto error;
		}
		return 0;
	}

	/* if there are no wildcards we don't need to search */
	if ((strchr(ps, '*') == NULL) && (strchr(ps, '?') == NULL)) {
		DBPRT(("%s: simple copy, no wildcards\n", __func__))
		rc = copy_file(dir, rmtflag, owner, source,
			       pair, conn, stage_inout, prmt, jobid);
		if (rc != 0) {
			snprintf(log_buffer, sizeof(log_buffer), "Job %s: no wildcards:%s stage%s failed for %s from %s to %s",
				 jobid, (rmtflag == 1) ? "remote" : "local", (dir == STAGE_DIR_OUT) ? "out" : "in", owner, source,
				 (dir == STAGE_DIR_OUT) ? pair->fp_rmt : pair->fp_local);
			log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, log_buffer);
			goto error;
		}
		return 0;
	}

	dirp = opendir(dname);
	if (dirp == NULL) { /* dir cannot be opened, just call copy_file */
		DBPRT(("%s: cannot open dir %s\n", __func__, dname))
		rc = copy_file(dir, rmtflag, owner, source,
			       pair, conn, stage_inout, prmt, jobid);
		if (rc != 0) {
			snprintf(log_buffer, sizeof(log_buffer), "Job %s: Cannot open directory:%s stage%s failed for %s from %s to %s",
				 jobid, (rmtflag == 1) ? "remote" : "local", (dir == STAGE_DIR_OUT) ? "out" : "in", owner, source,
				 (dir == STAGE_DIR_OUT) ? pair->fp_rmt : pair->fp_local);
			log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, log_buffer);
			goto error;
		}
		return 0;
	}

	while (errno = 0, (pdirent = readdir(dirp)) != NULL) {
#ifdef WIN32
		DWORD fa = 0;

		if (strcmp(pdirent->d_name, ".") == 0 ||
		    strcmp(pdirent->d_name, "..") == 0)
			continue;

		/* get Windows file attributes */
		pbs_strncpy(matched, dname, sizeof(matched));
		strcat(matched, pdirent->d_name);
		fa = GetFileAttributes(matched);

		/* skip windows HIDDEN or SYSTEM files */
		if (fa == INVALID_FILE_ATTRIBUTES)
			continue;
		if (fa == FILE_ATTRIBUTE_HIDDEN)
			continue;
		if (fa == FILE_ATTRIBUTE_SYSTEM)
			continue;

#else
		/* skip unix files that begin with '.' */
		if (pdirent->d_name[0] == '.')
			continue;
#endif
		if (pbs_glob(pdirent->d_name, ps) != 0) {
			/* name matches */

			pbs_strncpy(matched, dname, sizeof(matched));
			strcat(matched, pdirent->d_name);
			DBPRT(("%s: match %s\n", __func__, matched))
			rc = copy_file(dir, rmtflag, owner, matched,
				       pair, conn, stage_inout, prmt, jobid);
			if (rc != 0) {
				(void) closedir(dirp);
				snprintf(log_buffer, sizeof(log_buffer), "Job %s: Pattern matched:%s stage%s failed for %s from %s to %s",
					 jobid, (rmtflag == 1) ? "remote" : "local", (dir == STAGE_DIR_OUT) ? "out" : "in", owner, source,
					 (dir == STAGE_DIR_OUT) ? pair->fp_rmt : pair->fp_local);
				log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, log_buffer);
				goto error;
			}
		}
	}
	if (errno != 0 && errno != ENOENT) { /* dir cannot be read, just call copy_file */
		DBPRT(("%s: cannot read dir %s\n", __func__, dname))
		rc = copy_file(dir, rmtflag, owner, source,
			       pair, conn, stage_inout, prmt, jobid);
		(void) closedir(dirp);
		if (rc != 0) {
			snprintf(log_buffer, sizeof(log_buffer), "Job %s: Cannot read directory:%s stage%s failed for %s from %s to %s",
				 jobid, (rmtflag == 1) ? "remote" : "local", (dir == STAGE_DIR_OUT) ? "out" : "in", owner, source,
				 (dir == STAGE_DIR_OUT) ? pair->fp_rmt : pair->fp_local);
			log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, log_buffer);
			goto error;
		}
		return 0;
	}

	(void) closedir(dirp);
	return 0;

error:
	/* delete all the files in the list */
	for (i = 0; i < stage_inout->file_num; i++) {
		DBPRT(("%s: delete %s\n", __func__, stage_inout->file_list[i]))
		if (remtree(stage_inout->file_list[i]) != 0 && errno != ENOENT) {
			char temp[80 + MAXPATHLEN];

			sprintf(temp, msg_err_unlink, "stage in", stage_inout->file_list[i]);
			log_err(errno, "req_cpyfile", temp);
			add_bad_list(&(stage_inout->bad_list), temp, 2);
		}
	}
	return rc;
}

/**
 * @brief
 *	rmjobdir - Remove the staging and execution directory and any files
 *	within it.
 *
 * @param[in] jobid  - the job id string, i.e. "123.server"
 * @param[in] jobdir - the full path to the sandox (working directory to make
 * @param[in] uid    - the user id of the user under which the job will run.
 *		       Currenty this parameter only used in *nix so for
 *		       windows this parameter should be NULL
 * @param[in] gid    - the group id of the user under which the job will run.
 *		       Currenty this parameter only used in *nix so for
 *		       windows this parameter should be NULL
 * @param[in] check_shared - if set to '1', test if the staging and execution
 *			directory is sitting on a shared location, and if so,
 *			do not remove any files in it.
 * @return void
 *
 * @note	This may take awhile so the task is forked and execed to another
 *		process. In *nix, as with mkjobdir(),  the actions must be done
 *		as the User or as root depending on the location of the sandbox.
 *
 */
void
rmjobdir(char *jobid, char *jobdir, uid_t uid, gid_t gid, int check_shared)
{
	static char rmdir_buf[MAXPATHLEN + 1] = {'\0'};
	struct stat sb = {0};
	char *newdir = NULL;
	char *nameptr = NULL;
#ifdef WIN32
	struct pio_handles pio = {0};
	char cmdbuf[MAXPATHLEN + 1] = {'\0'};
	char sep = '\\';
#else
	pid_t pid = -1;
	char *rm = "/bin/rm";
	char *rf = "-rf";
	char sep = '/';
#endif

	if (jobdir == NULL)
		return;

	if (check_shared &&
	    (pbs_jobdir_root[0] != '\0') &&
	    pbs_jobdir_root_shared &&
	    ((strcmp(pbs_jobdir_root, JOBDIR_DEFAULT) == 0) ||
	     (strncmp(pbs_jobdir_root, jobdir, strlen(pbs_jobdir_root)) == 0))) {
		log_eventf(PBSEVENT_DEBUG3, PBS_EVENTCLASS_JOB, LOG_DEBUG, jobid ? jobid : "", "shared jobdir %s to be removed by primary mom", jobdir);

		return;
	}

#ifndef WIN32
	if ((pbs_jobdir_root[0] == '\0') || (strcmp(pbs_jobdir_root, JOBDIR_DEFAULT) == 0)) {
		/* In user's home, need to be user */
		/* The rest must be done as the User */
		if (impersonate_user(uid, gid) == -1)
			return;
	}
#endif

	/* Hello, is any body there? */
	if (stat(jobdir, &sb) == -1) {
#ifndef WIN32
		if ((pbs_jobdir_root[0] == '\0') || (strcmp(pbs_jobdir_root, JOBDIR_DEFAULT) == 0)) {
			/* oops, have to go back to being root */
			revert_from_user();
		}
#endif
		if (errno != ENOENT) {
			sprintf(log_buffer, "stat: %s", jobdir);
			log_joberr(errno, __func__, log_buffer, jobid);
		}
		return;
	}

	/*
	 * The job path should be "pbs.<jobid>.x8z"
	 * if the string "pbs." is not found in the basename of
	 * the given path, then there is no staging and execution
	 * directory.  So don't delete anything, we don't want
	 * to remove too much!
	 * There is no need to check for trailing separator at the
	 * end of the job path, because jobdirname() does not add
	 * a trailing separator.
	 * NOTE: basename() wasn't available on all supported architectures,
	 * so instead we look for the last separator in the path name,
	 * and advance the pointer one space (past the
	 * separator) to get the basename.
	 */
	if ((nameptr = strrchr(jobdir, sep)) != NULL) {
		nameptr++;
	} else {
		/* we already have the basename */
		nameptr = jobdir;
	}

	if (strncmp(nameptr, "pbs.", 4) != 0) {
#ifndef WIN32
		if ((pbs_jobdir_root[0] == '\0') || (strcmp(pbs_jobdir_root, JOBDIR_DEFAULT) == 0))
			revert_from_user();
#endif
		sprintf(log_buffer, "%s is not a staging and execution directory", jobdir);
		log_joberr(-1, __func__, log_buffer, jobid);
		return;
	}

	snprintf(rmdir_buf, sizeof(rmdir_buf) - 1, "%s_remove", jobdir);
	newdir = rmdir_buf;

#ifdef WIN32
	make_dir_files_service_account_read(jobdir);

	if (!MoveFile(jobdir, newdir)) {
		errno = GetLastError();
		sprintf(log_buffer, "rename: %s %s", jobdir, newdir);
		log_joberr(errno, __func__, log_buffer, jobid);
		newdir = jobdir;
	}

	sprintf(cmdbuf, "rmdir /S /Q \"%s\"", newdir);
	if (!win_popen(cmdbuf, "w", &pio, NULL)) {
		errno = GetLastError();
		log_joberr(errno, __func__, "win_popen", jobid);
	}
	win_pclose2(&pio);
	close_valid_handle(&(pio.pi.hProcess));
#else
	if (rename(jobdir, newdir) == -1) {
		sprintf(log_buffer, "rename: %s %s", jobdir, newdir);
		log_joberr(errno, __func__, log_buffer, jobid);
		newdir = jobdir;
	}

	/* fork and exec the cleantmp process */
	pid = fork();
	if (pid != 0) { /* parent or error */
		int err = errno;
		if ((pbs_jobdir_root[0] == '\0') || (strcmp(pbs_jobdir_root, JOBDIR_DEFAULT) == 0))
			revert_from_user();

		if (pid < 0)
			log_err(err, __func__, "fork");
		return;
	}

	tpp_terminate();
	execl(rm, "pbs_cleandir", rf, newdir, NULL);
	log_err(errno, __func__, "execl");
	exit(21);
#endif
}

#ifndef WIN32

/**
 * @brief
 *	copy string quoting whitespace by prefixing with back-slash
 *
 * @par Functionality:
 *	Copy a source string into a buffer.  Any whitespace as defined by
 *	"isspace()" is prefixed by a back-slash '\'.
 *
 * @par Note
 *	this is compiled for Unix/Linux only.   Windows has it's own thing
 *	in lib/Libwin.
 *
 * @param[in]	pd  - input string
 * @param[in]	ps  - output buffer into which the coping is done
 * @param[in]	sz  - length of output buffer
 *
 * @return	int
 * @retval	0 : 	copied successfully
 * @retval	1 :	buffer would have overflowed, buf is not null terminated
 *
 */

static int
quote_and_copy_white(char *pd, char *ps, ssize_t sz)
{

	/* Copy the file path escaping white space with a back-slash */

	while (*ps) {
		if (isspace((int) *ps)) {
			*pd++ = '\\';
			if (--sz < 1)
				return 1;
		}
		*pd++ = *ps++; /* copy the character */
		if (--sz < 1)
			return 1;
	}
	*pd = '\0';
	return 0;
}
#else
/**
 * @brief
 *	is_scp_path - check whether scp_path in pbs_conf is set and contain "scp"
 *
 * @return	int
 * @retval	0 - either scp_path is not set or does not contain "scp"
 * @retval	1 - scp_path is set and contain "scp"
 *
 */
static int
is_scp_path(void)
{
	if (pbs_conf.scp_path &&
	    (strstr(pbs_conf.scp_path, "scp") ||
	     strstr(pbs_conf.scp_path, "SCP") ||
	     strstr(pbs_conf.scp_path, "Scp"))) {
		return (1);
	}
	return (0);
}

/**
 * @brief
 *	is_pbs_rcp_path - check whether rcp_path in pbs_conf is set and contain "pbs_rcp"
 *
 * @return	int
 * @retval	0 - either rcp_path is not set or does not contain "pbs_rcp"
 * @retval	1 - rcp_path is set and contain "pbs_rcp"
 *
 */
static int
is_pbs_rcp_path(void)
{
	if (pbs_conf.rcp_path &&
	    (strstr(pbs_conf.rcp_path, "pbs_rcp") ||
	     strstr(pbs_conf.scp_path, "PBS_RCP") ||
	     strstr(pbs_conf.scp_path, "Pbs_rcp"))) {
		return (1);
	}
	return (0);
}
#endif
/**
 * @brief
 *	sys_copy
 *	issue system call to copy file
 *	Also check error of copy process and retry as required
 *
 *	In Windows, use "xcopy" (for directory) or "copy" (for single file)
 *	for local copy and "pbs_rcp" for remote copy.
 *	If there is an error in the copy and pbs_rcp is used, it will try with scp.
 *
 *	In *nix, use "cp" for local copy and "scp"/"rcp" for remote copy.
 *	If there is an error in the copy and scp is used, it will try with rcp.
 *
 *	If there is an error, the copy will be retried 3 additional times.
 *
 * @param[in]		dir		-	direction of copy
 *						STAGE_DIR_IN - for stage in request
 *						STAGE_DIR_OUT - for stageout request
 * @param[in]		rmtflag		-	is remote file copy
 * @param[in]		owner		-	username for owner of copy request
 * @param[in]		src		-	path to source is stageout else local file name
 * @param[in]		pair		-	list of file pair
 * @param[in]		conn		-	socket on which request is received
 * @param[in]		prmt		-	path to destination if stageout else source path
 * @param[in]		jobid		-	Job ID
 *
 * @return	int
 * @retval	0 - successful copy
 * @retval 	!0 - copy failed
 *
 * @note
 * Use of the arguments is somewhat confusing, in summary the files copied are:
 *	STAGE_DIR_OUT
 *		"src"  local source file to copy
 *		"prmt" local or remote destination file
 *	STAGE_DIR_IN
 *		if remote - "prmt" is the remote source file
 *		if local  - "src"  is the local source file
 *		"pair->fp_local" is the local destination path in both cases
 *
 */
static int
sys_copy(int dir, int rmtflg, char *owner, char *src, struct rqfpair *pair, int conn, char *prmt, char *jobid)
{
	char *ag0 = NULL;
	char *ag1 = NULL;
	char ag2[MAXPATHLEN + 1] = {'\0'};
	char ag3[MAXPATHLEN + 1] = {'\0'};
	char local_file[MAXPATHLEN + 1] = {'\0'};
	char rmt_file[MAXPATHLEN + 1] = {'\0'};
	int loop = 0;
	int rc = 0;
	time_t original = 0;
	struct stat sb = {0};
#ifdef WIN32
	char str_buf[MAXPATHLEN + 1] = {'\0'};
	char *sp = NULL;
	char *dp = NULL;
	int fd = -1;
	STARTUPINFO si = {0};
	PROCESS_INFORMATION pi = {0};
	int flags = CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE |
		    CREATE_NEW_PROCESS_GROUP;
	char cmd_line[PBS_CMDLINE_LENGTH] = {'\0'};
	char ag2_path[MAXPATHLEN + 1] = {'\0'};
	char ag3_path[MAXPATHLEN + 1] = {'\0'};
	char wdir[MAXPATHLEN + 1] = {'\0'};
	HANDLE readp = INVALID_HANDLE_VALUE;
	HANDLE writep = INVALID_HANDLE_VALUE;
	SECURITY_ATTRIBUTES sa = {0};
	struct passwd *pw = NULL;
#else
	int i;
	ssize_t len;
#endif

	DBPRT(("%s: %s %s copy %s of %s\n", __func__, owner,
	       rmtflg ? "remote" : "local",
	       (dir == STAGE_DIR_OUT) ? "out" : "in", src))

#ifdef WIN32
	ZeroMemory(&si, sizeof(SYSTEM_INFO));
	sa.nLength = sizeof(sa);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;

	strcpy(wdir, "C:\\");

	if (getcwd(wdir, MAXPATHLEN + 1) == NULL) {
		log_errf(-1, __func__, "Failed to get the current working directory %s", wdir);
	}

	si.cb = sizeof(si);
	si.lpDesktop = PBS_DESKTOP_NAME;

	if ((pw = getpwnam(owner)) == NULL) {
		log_errf(-1, __func__, "Failed to get %s password", owner);
		rc = PBSE_BADUSER;
		goto sys_copy_end;
	}
#endif
	sprintf(rcperr, "%srcperr.%d", path_spool, getpid());

	if (dir == STAGE_DIR_OUT) {
#ifndef WIN32
		if (*src == '-')
			strcpy(ag2, "./"); /* prefix leading '-' with "./" */
#endif
		replace(src, "\\,", ",", local_file);
		if (*local_file != '\0')
			strcpy(ag2, local_file);
		else
			pbs_strncpy(ag2, src, sizeof(ag2));

#ifndef WIN32
		/* Is the file there?  If not, don`t try copy */
		if (access(ag2, F_OK | R_OK) < 0)
#else
		if (access_uncpath(ag2, F_OK | R_OK) < 0)
#endif
		{
			if (errno == ENOENT)
				return 1;
		}

		/* take (remote) destination name from request */
		if (rmtflg) {
#ifdef WIN32
			/* using rcp, need to prepend the owner name */
			if (is_scp_path() || is_pbs_rcp_path()) {
				strcat(ag3, owner);
				strcat(ag3, "@");
				replace(prmt, "\\,", ",", rmt_file);
				if (*rmt_file != '\0')
					strcat(ag3, rmt_file);
				else
					strcat(ag3, prmt);
			} else {
				for (dp = ag3, sp = prmt; *sp; dp++, sp++) {
					if (*sp == ':')
						break;
					*dp = *sp;
				}
				*dp++ = '.';
				strcpy(dp, owner);
				strcat(dp, ":");
				strcat(dp, sp + 1);
			}
#else
			/* using scp/rcp, need to prepend the owner name */
			strcat(ag3, owner);
			strcat(ag3, "@");
			len = strlen(ag3);
			if (quote_and_copy_white(ag3 + len, prmt, MAXPATHLEN - len) != 0)
				return 1;
#endif
		} else {
#ifndef WIN32
			if (*prmt == '-')
				strcat(ag3, "./"); /* prefix '-' with "./" */
#endif
			replace(prmt, "\\,", ",", rmt_file);
			if (*rmt_file != '\0')
				strcat(ag3, rmt_file);
			else
				strcat(ag3, prmt);
		}
	} else { /* in bound (stage-in) file */
		/* take (remote) source name from request */
		if (rmtflg) {
#ifdef WIN32
			/* using rcp, need to prepend the owner name */
			if (is_scp_path() || is_pbs_rcp_path()) {
				strcat(ag2, owner);
				strcat(ag2, "@");
				replace(prmt, "\\,", ",", rmt_file);
				if (*rmt_file != '\0')
					strcat(ag2, rmt_file);
				else
					strcat(ag2, prmt);
			} else {
				for (dp = ag2, sp = prmt; *sp; dp++, sp++) {
					if (*sp == ':')
						break;
					*dp = *sp;
				}
				*dp++ = '.';
				strcpy(dp, owner);
				strcat(dp, ":");
				strcat(dp, sp + 1);
			}
#else
			/* using scp/rcp, need to prepend the owner name */
			pbs_strncpy(ag2, owner, sizeof(ag2));
			strcat(ag2, "@");
			len = strlen(ag2);
			if (quote_and_copy_white(ag2 + len, prmt, MAXPATHLEN - len) != 0)
				return 1;
#endif
		} else {
#ifndef WIN32
			if (*src == '-')
				strcpy(ag2, "./"); /* prefix '-' with "./" */
#endif
			replace(src, "\\,", ",", rmt_file);
			if (*rmt_file != '\0')
				strcat(ag2, rmt_file);
			else
				strcat(ag2, src);
		}
#ifndef WIN32
		if (*pair->fp_local == '-')
			strcpy(ag3, "./"); /* prefix leading '-' with "./" */
#endif
		replace(pair->fp_local, "\\,", ",", local_file);
		if (*local_file != '\0')
			strcpy(ag3, local_file);
		else
			pbs_strncpy(ag3, pair->fp_local, sizeof(ag3));
	}

#ifndef WIN32
	for (loop = 1; loop < 5; ++loop) {
		original = 0;
		if (rmtflg == 0) { /* local copy */
			ag0 = pbs_conf.cp_path;
			if (strcmp(ag3, "/dev/null") == 0)
				return (0); /* don't need to copy, just return zero */
			else
				ag1 = "-rp";

			/* remote, try scp */
		} else if (pbs_conf.scp_path != NULL && (loop % 2) == 1) {
			ag0 = pbs_conf.scp_path;
			if (pbs_conf.scp_args != NULL) {
				ag1 = pbs_conf.scp_args;
                        } else {
				ag1 = "-Brvp";
                        }
		} else {
			ag0 = pbs_conf.rcp_path;
			ag1 = "-rp";
		}

		/*
		 * There is a problem where scp can return zero indicating success even
		 * though the copy did not work.  In the case of a remote stagein,
		 * where the file already exists, save the ctime of an existing file so
		 * we can check the ctime after the copy to be sure it worked.  ctime
		 * is used instead of mtime because scp may reset mtime.
		 */
		if ((rmtflg != 0) && (dir == STAGE_DIR_IN)) {
			if (stat(ag3, &sb) != -1)
				original = sb.st_ctime;
		}

		DBPRT(("%s: %s %s %s %s\n", __func__, ag0, ag1, ag2, ag3))

		if ((rc = fork()) > 0) {

			/* Parent */
			if (cred_pipe != -1) {
				if (write(cred_pipe, pwd_buf, cred_len) != cred_len) {
					log_err(errno, __func__, "pipe write");
				}
			}

			/* wait for copy to complete */
			while (((i = wait(&rc)) < 0) && (errno == EINTR))
				;
			if (i == -1) {
				rc = (20000 + errno); /* 200xx is error on wait */
			} else if (WIFEXITED(rc)) {
				if ((rc = WEXITSTATUS(rc)) == 0) {
					if ((rmtflg != 0) && (dir == STAGE_DIR_IN)) {
						if ((stat(ag3, &sb) == -1) ||
						    (original == sb.st_ctime))
							rc = 13;
					}
					return (rc); /* good,  stop now */
				}
			} else if (WIFSTOPPED(rc)) {
				rc = (30000 + WSTOPSIG(rc)); /* 300xx is stopped */
			} else if (WIFSIGNALED(rc)) {
				rc = (40000 + WTERMSIG(rc)); /* 400xx is signaled */
			}

		} else if (rc < 0) {

			rc = errno + 10000; /* error on fork (100xx), retry */

		} else {

			int fd;

			/* child - exec the copy command */

			(void) close(conn);

			/* redirect stderr to make error from rcp available to MOM */
			if ((fd = open(rcperr, O_RDWR | O_CREAT, 0644)) < 0) {
				(void) sprintf(log_buffer, "can't open %s, error = %d",
					       rcperr, errno);
				log_event(PBSEVENT_DEBUG, PBS_EVENTCLASS_FILE,
					  LOG_DEBUG, __func__, log_buffer);
				exit(12);
			};
			if (fd != 2) {
				(void) dup2(fd, 2);
				(void) close(fd);
			}
			/*
			 * In order to fix a timing problem where the copy may have
			 * succeeded in less than a second, the child process may have to
			 * sleep for 1 second before doing the copy.  This will ensure the
			 * ctime will be different. This is needed because the exit value
			 * of the copy agent can be zero even when the copy did not work
			 * correctly.
			 *
			 * The value of original will be 0 or the previously existing
			 * file's ctime.  If this ctime is "right now", then we have to
			 * sleep a second so we can tell if the copy worked.  sleep() can
			 * be terminated early if a signal is received, so loop until the
			 * current time is different than the initial ctime of the file.
			 */
			while (original == time(NULL)) {
				sleep(1);
			}

			execl(ag0, ag0, ag1, ag2, ag3, NULL);
			sprintf(log_buffer, "command: %s %s %s %s execl failed %d", ag0, ag1, ag2, ag3, errno);
			log_event(PBSEVENT_DEBUG, PBS_EVENTCLASS_FILE, LOG_DEBUG, __func__, log_buffer);
			exit(13); /* 13, an unluckly number */
		}

		/* copy did not work, try again */

		sprintf(log_buffer, "command: %s %s %s %s status=%d, try=%d", ag0, ag1, ag2, ag3, rc, loop);
		log_event(PBSEVENT_DEBUG, PBS_EVENTCLASS_FILE, LOG_DEBUG, __func__, log_buffer);
		if ((loop % 2) == 0) /* don't sleep between scp and rcp */
			sleep(loop / 2 * 10 + 1);
	}
#else
	for (loop = 1; loop < 5; ++loop) {
		original = 0;
		if (rmtflg == 0) { /* local copy */
			ag0 = pbs_conf.cp_path;
			ag1 = "/e/i/q/y";
			/* remote, try scp */
		} else if (pbs_conf.scp_path != NULL && (loop % 2) == 1) {
			ag0 = pbs_conf.scp_path;
			struct stat local_sb = {0};
			char ag0_cpy[MAXPATHLEN + 1] = {'\0'};
			pbs_strncpy(ag0_cpy, ag0, sizeof(ag0_cpy));
			if (stat_uncpath(ag0_cpy, &local_sb) == -1) {
				log_errf(errno, __func__, "%s", ag0_cpy);
				/* let's try to copy using pbs_rcp now */
				continue;
			}
			if (pbs_conf.scp_args != NULL) {
				ag1 = pbs_conf.scp_args;
                        } else {
				ag1 = "-Brv";
                        }
		} else {
			ag0 = pbs_conf.rcp_path;
			if ((cred_buf != NULL) && (cred_len != 0)) {
				int clen;
				int wrote;

				ag1 = "-E -r";

				if (CreatePipe(&readp, &writep, &sa, 0) == 0) {
					log_err(-1, __func__, "Unable to create pipe");
					rc = 22;
					goto sys_copy_end;
				}
				clen = sizeof(size_t);
				WriteFile(writep, &cred_len, clen, &wrote, NULL);

				if (wrote != clen) {
					log_errf(-1, __func__, "sending cred_len: wrote %d should be %d", wrote, clen);
					rc = 22;
					goto sys_copy_end;
				}
				WriteFile(writep, cred_buf, cred_len, &wrote, NULL);
				if (wrote != cred_len) {
					log_errf(-1, __func__, "sending cred_buf: wrote %d should be %d", wrote, clen);
					rc = 22;
					goto sys_copy_end;
				}
			} else {
				ag1 = "-r";
			}
		}

		/*
		 ** There is a problem where scp can return zero
		 ** indicating success even though the copy did
		 ** not work.  In the case of a remote stagein,
		 ** save the mtime of an existing file so we can
		 ** check the mtime after the copy to be sure it
		 ** worked.
		 */
		if ((rmtflg != 0) && (dir == STAGE_DIR_IN)) {
			if (stat_uncpath(ag3, &sb) != -1)
				original = sb.st_mtime;
		}

		/* redirect stderr to make error from rcp available to MOM */
		if ((fd = open(rcperr, O_RDWR | O_CREAT, 0644)) < 0) {
			log_errf(errno, __func__, "can't open %s", rcperr);
			rc = 12;
		} else {
			errno = 0;

			if (rmtflg == 0) {
				/*
				 *  Xcopy Implementation.
				 */
				replace(ag2, "\\ ", " ", str_buf);
				snprintf(ag2_path, sizeof(ag2_path),
					 "%s", str_buf);
				fix_path(ag2_path, 3);

				if (stat_uncpath(ag2, &sb) != -1) {
					if (S_ISREG(sb.st_mode)) {
						replace(ag3, "\\ ", " ", str_buf);
						snprintf(ag3_path, sizeof(ag3_path), "%s",
							 str_buf);
						fix_path(ag3_path, 3);

						/* if file, use copy with /y option */
						/* the option /y will supress any file
						 overwrite messages */
						ag0 = "copy";
						ag1 = "/y";
						snprintf(cmd_line, sizeof(cmd_line),
							 "cmd /c %s %s \"%s\" \"%s\"",
							 ag0, ag1, ag2_path, ag3_path);
					} else {
						char ag3_tmp[MAXPATHLEN + 1];
						char *ss, *ts;
						size_t len;

						pbs_strncpy(ag3_tmp, ag3, sizeof(ag3_tmp));
						len = strlen(ag3_tmp);

						/*
						 * Handle case where destination path has
						 * a trailing slash.
						 */
						if (len > 0 && ag3_tmp[len - 1] == '/') {
							len--;
							ag3_tmp[len] = '\0';
						}
						/*
						 * Compare the last component of the source
						 * to the last component of the target to
						 * see if they match.
						 */
						ss = strrchr(ag2, '/');
						ss = ss ? ss + 1 : ag2;
						ts = strrchr(ag3_tmp, '/');
						ts = ts ? ts + 1 : ag3_tmp;

						if (strcmp(ss, ts) != 0) {
							/*
							 * The last path components are different
							 * so add last component of source directory
							 * to target since xcopy will not do it.
							 */
							strncat(ag3_tmp, "/",
								sizeof(ag3_tmp) - len - 1);
							strncat(ag3_tmp, ss,
								sizeof(ag3_tmp) - len - 2);
						}
						replace(ag3_tmp, "\\ ", " ", str_buf);
						snprintf(ag3_path, sizeof(ag3_path), "%s",
							 str_buf);
						fix_path(ag3_path, 3);
						snprintf(cmd_line, sizeof(cmd_line),
							 "cmd /c %s %s \"%s\" \"%s\"",
							 ag0, ag1, ag2_path, ag3_path);
					}
				} else {
					log_errf(errno, __func__, "%s", ag2);
					rc = errno;
					goto sys_copy_end;
				}
			} else {
				si.dwFlags = STARTF_USESTDHANDLES;

				if (readp != INVALID_HANDLE_VALUE)
					si.hStdInput = readp;
				else
					si.hStdInput = INVALID_HANDLE_VALUE;

				si.hStdOutput = INVALID_HANDLE_VALUE;
				si.hStdError = (HANDLE) _get_osfhandle(fd);

				if (ag2[1] == ':') { /* has drive info */

					sprintf(wdir, "%c:\\", toupper(ag2[0]));
					if (_getdcwd(toupper(ag2[0]) - 'A' + 1, wdir, sizeof(wdir)) == NULL) {
						log_errf(errno, __func__, "Failed to get the full path of the current working directory on the specified drive %s", wdir);
					}
					strcpy(ag2_path, replace_space(ag2 + 2, ""));
					fix_path(ag2_path, 3);
				} else if (strchr(ag2, ':')) {
					/* replace "\ " wth " " so "\" not forwarded */
					replace(ag2, "\\ ", " ", ag2_path);
					fix_path(ag2_path, 1);
					sprintf(ag2_path, "\"%s\"",
						replace_space(ag2_path, "\\ "));
				} else {
					sprintf(ag2_path, "\"%s\"", ag2);
					fix_path(ag2_path, 3);
				}

				if (ag3[1] == ':') { /* has drive info */

					sprintf(wdir, "%c:\\", toupper(ag3[0]));

					strcpy(ag3_path, replace_space(ag3 + 2, ""));
					fix_path(ag3_path, 3);
				} else if (strchr(ag3, ':')) {
					/* replace "\ " wth " " so "\" not forwarded */
					replace(ag3, "\\ ", " ", ag3_path);
					fix_path(ag3_path, 1);
					sprintf(ag3_path, "\"%s\"",
						replace_space(ag3_path, "\\ "));
				} else {
					sprintf(ag3_path, "\"%s\"", ag3);
					fix_path(ag3_path, 3);
				}

				sprintf(cmd_line, "%s %s %s %s", ag0, ag1,
					ag2_path, ag3_path);
			}

			/*
			 ** In order to fix a timing problem where the copy may
			 ** have succeeded in less than a second, the child
			 ** process may have to sleep for 1 second before doing
			 ** the copy.  This will ensure the mtime will be different.
			 ** This is needed because the exit value of
			 ** the copy agent can be zero even when the copy
			 ** did not work correctly.
			 **
			 ** The value of original will be 0 or the previously
			 ** existing file's mtime.  If this mtime is "right now",
			 ** then we have to sleep a second so we can tell if
			 ** the copy worked.
			 */
			if (original == time(NULL))
				sleep(1);
			/* Re-initialize errno to zero */
			errno = 0;

			/* do we need 'AsUser' here? */
			if ((pw->pw_userlogin == INVALID_HANDLE_VALUE) && (strcmpi(owner, getlogin()) == 0)) {
				snprintf(log_buffer, sizeof(log_buffer), "Job %s: CreateProcess(%s) under acct %s wdir=%s",
					 jobid, cmd_line, owner, wdir);
				log_event(PBSEVENT_DEBUG, PBS_EVENTCLASS_JOB, LOG_DEBUG, __func__, log_buffer);
				rc = CreateProcess(NULL, cmd_line,
						   NULL, NULL, TRUE, flags,
						   NULL, wdir, &si, &pi);
				if (rc == 0) {
					errno = GetLastError();
					log_err(-1, __func__, "CreateProcess failed");
				}
			} else {
				snprintf(log_buffer, sizeof(log_buffer), "Job %s: CreateProcessAsUser(%d, %s) under acct %s wdir=%s",
					 jobid, pw->pw_userlogin, cmd_line, getlogin(), wdir);
				log_event(PBSEVENT_DEBUG, PBS_EVENTCLASS_JOB, LOG_DEBUG, __func__, log_buffer);
				rc = CreateProcessAsUser(pw->pw_userlogin, NULL, cmd_line,
							 NULL, NULL, TRUE, flags,
							 NULL, wdir, &si, &pi);
				if (rc == 0) {
					errno = GetLastError();
					log_err(-1, __func__, "CreateProcessAsUser failed");
				}
			}

			if (rc == 0) {
				errno = GetLastError();
				snprintf(log_buffer, sizeof(log_buffer), "Job %s: process creation failed errno %lu", jobid, errno);
				log_err(-1, __func__, log_buffer);
			}

			close(fd);
			fd = -1; /* already done the close */

			if (errno != 0) {
				rc = errno + 10000; /* error on fork (100xx), retry */
			} else {
				int ret = 0;
				ret = WaitForSingleObject(pi.hProcess, INFINITE);
				if (ret != WAIT_OBJECT_0) {
					if (ret != WAIT_FAILED) {
						sprintf(log_buffer, "Job %s: WaitForSingleObject failed with status=%d", jobid, ret);
						log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, log_buffer);
					} else {
						sprintf(log_buffer, "Job %s: WaitForSingleObject failed with status=%d", jobid, ret);
						log_err(-1, __func__, log_buffer);
					}
				}
				if (!GetExitCodeProcess(pi.hProcess, &rc)) {
					sprintf(log_buffer, "Job %s: GetExitCodeProcess failed", jobid);
					log_err(-1, __func__, log_buffer);
				}
				if (rc != 0) {
					sprintf(log_buffer, "Job %s: GetExitCodeProcess return code=%d", jobid, rc);
					log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_JOB, LOG_ERR, __func__, log_buffer);
				}
				CloseHandle(pi.hProcess);
				CloseHandle(pi.hThread);

				if (errno != 0) {
					rc = (20000 + errno); /* 200xx is error on wait */
				} else {
					if ((rmtflg != 0) && (dir == STAGE_DIR_IN)) {
						if ((stat_uncpath(ag3, &sb) == -1) ||
						    (original == sb.st_mtime))
							rc = 13;
					}
					goto sys_copy_end;
				}
			}
		}

		/* copy did not work, try again */
		(void) sprintf(log_buffer,
			       "command: %s %s %s %s status=%d, try=%d",
			       ag0, ag1, ag2, ag3, rc, loop);
		log_event(PBSEVENT_DEBUG, PBS_EVENTCLASS_FILE, LOG_DEBUG,
			  __func__, log_buffer);
		if ((loop % 2) == 0) /* don't sleep between scp and rcp */
			Sleep(1000 * (loop / 2 * 10 + 1));
	}

sys_copy_end:
	if (readp != INVALID_HANDLE_VALUE) {
		CloseHandle(readp);
	}
	if (writep != INVALID_HANDLE_VALUE) {
		CloseHandle(writep);
	}

	if (fd != -1)
		(void) close(fd);
#endif
	return (rc); /* tried a bunch of times, just give up */
}

#ifdef WIN32
/**
 * @brief
 *	free_pcphosts - Free allocated cphosts struct (i.e. pcphosts)
 *
 * @return void
 *
 */
static void
free_pcphosts(void)
{
	int nh = 0;

	if (pcphosts != NULL && cphosts_num > 0) {
		for (nh = 0; nh < cphosts_num; nh++) {
			if ((pcphosts + nh)->cph_hosts != NULL) {
				free((pcphosts + nh)->cph_hosts);
				(pcphosts + nh)->cph_hosts = NULL;
			}

			if ((pcphosts + nh)->cph_from != NULL) {
				free((pcphosts + nh)->cph_from);
				(pcphosts + nh)->cph_from = NULL;
			}

			if ((pcphosts + nh)->cph_to != NULL) {
				free((pcphosts + nh)->cph_to);
				(pcphosts + nh)->cph_to = NULL;
			}
		}

		free(pcphosts);
		pcphosts = NULL;
		cphosts_num = 0;
	}
}

/**
 * @brief
 *	recv_pcphosts - Receive cphosts struct through stdin sent by parent MoM
 *
 * @return	int
 * @retval	0 - on Error
 * @retval	1 - on Success
 *
 * @note	This function will put received info in
 *		global struct pcphosts and cphosts_num variable
 *
 */
int
recv_pcphosts(void)
{
	char buf[CPY_PIPE_BUFSIZE] = {'\0'};
	int nh = 0;

	if (fgets(buf, sizeof(int), stdin) == NULL) {
		log_err(-1, __func__, "Failed to read cphosts_num");
		return 0;
	} else {
		buf[strlen(buf) - 1] = '\0';
		cphosts_num = (unsigned) atoi(buf);
	}

	if (cphosts_num <= 0) {
		return 1;
	} else {
		if ((pcphosts = malloc(cphosts_num * sizeof(struct cphosts))) == NULL) {
			log_err(errno, __func__, "malloc failed");
			return 0;
		} else {
			memset(pcphosts, 0, (cphosts_num * sizeof(struct cphosts)));
		}
	}

	for (nh = 0; nh < cphosts_num; nh++) {

		if (fgets(buf, sizeof(buf), stdin) == NULL) {
			free_pcphosts();
			log_err(-1, __func__, "Failed to read cph_hosts");
			return 0;
		} else {
			buf[strlen(buf) - 1] = '\0';
			if (((pcphosts + nh)->cph_hosts = strdup(buf)) == NULL) {
				log_err(-1, __func__, "Failed to allocate data to cph_hosts");
				free_pcphosts();
				return 0;
			} else
				memset(buf, 0, sizeof(buf));
		}

		if (fgets(buf, sizeof(buf), stdin) == NULL) {
			free_pcphosts();
			log_err(-1, __func__, "Failed to read cph_from");
			return 0;
		} else {
			buf[strlen(buf) - 1] = '\0';
			if (((pcphosts + nh)->cph_from = strdup(buf)) == NULL) {
				log_err(-1, __func__, "Failed to allocate data to cph_from");
				free_pcphosts();
				return 0;
			} else
				memset(buf, 0, sizeof(buf));
		}

		if (fgets(buf, sizeof(buf), stdin) == NULL) {
			log_err(-1, __func__, "Failed to read cph_to");
			free_pcphosts();
			return 0;
		} else {
			buf[strlen(buf) - 1] = '\0';
			if (((pcphosts + nh)->cph_to = strdup(buf)) == NULL) {
				log_err(-1, __func__, "Failed to allocate data to cph_to");
				free_pcphosts();
				return 0;
			} else
				memset(buf, 0, sizeof(buf));
		}
	}
	return 1;
}

/**
 * @brief
 *	free_rq_cpyfile_cred - Free allocated rq_cpyfile and cred info (if any)
 *
 * @param[in] pcf - pointer to already allocated rq_cpyfile struct
 *
 * @return void
 *
 */
static void
free_rq_cpyfile_cred(struct rq_cpyfile *pcf)
{
	if (pcf != NULL) {
		struct rqfpair *ppair = NULL;

		while ((ppair = (struct rqfpair *) GET_NEXT(pcf->rq_pair)) != NULL) {
			delete_link(&ppair->fp_link);
			if (ppair->fp_local)
				(void) free(ppair->fp_local);
			if (ppair->fp_rmt)
				(void) free(ppair->fp_rmt);
			(void) free(ppair);
		}
	}

	if (cred_buf != NULL) {
		memset(cred_buf, 0, cred_len);
		free(cred_buf);
		cred_buf = NULL;
		cred_len = 0;
	}
}

/**
 * @brief
 *	recv_rq_cpyfile_cred - Receive rq_cpyfile struct and cred_info (if any) through stdin sent by parent MoM
 *
 * @param[in/out] pcf - pointer to already allocated rq_cpyfile struct
 *
 * @return	int
 * @retval	0 - on Error
 * @retval	1 - on Success
 *
 * @note	This function will put received cred info (if any) in
 *		global variable cred_buf and cred_len
 *
 */
int
recv_rq_cpyfile_cred(struct rq_cpyfile *pcf)
{
	int pair_ct = 0;
	struct rqfpair *ppair = NULL;
	char buf[CPY_PIPE_BUFSIZE] = {'\0'};

	CLEAR_HEAD(pcf->rq_pair);

	if (fgets(pcf->rq_jobid, PBS_MAXJOBNAME, stdin) == NULL) {
		log_err(-1, __func__, "Failed to read rq_jobid");
		return 0;
	} else {
		pcf->rq_jobid[strlen(pcf->rq_jobid) - 1] = '\0';
	}

	if (fgets(pcf->rq_owner, PBS_MAXUSER, stdin) == NULL) {
		log_err(-1, __func__, "Failed to read rq_owner");
		return 0;
	} else {
		pcf->rq_owner[strlen(pcf->rq_owner) - 1] = '\0';
	}

	if (fgets(pcf->rq_user, PBS_MAXUSER, stdin) == NULL) {
		log_err(-1, __func__, "Failed to read rq_user");
		return 0;
	} else {
		pcf->rq_user[strlen(pcf->rq_user) - 1] = '\0';
	}

	if (fgets(pcf->rq_group, PBS_MAXGRPN, stdin) == NULL) {
		log_err(-1, __func__, "Failed to read rq_group");
		return 0;
	} else {
		pcf->rq_group[strlen(pcf->rq_group) - 1] = '\0';
	}

	if (fgets(buf, sizeof(int), stdin) == NULL) {
		log_err(-1, __func__, "Failed to read rq_dir");
		return 0;
	} else {
		buf[strlen(buf) - 1] = '\0';
		pcf->rq_dir = (unsigned) atoi(buf);
	}

	if (fgets(buf, sizeof(int), stdin) == NULL) {
		log_err(-1, __func__, "Failed to read pair_ct");
		return 0;
	} else {
		buf[strlen(buf) - 1] = '\0';
		pair_ct = (unsigned) atoi(buf);
	}

	while (pair_ct--) {

		ppair = (struct rqfpair *) malloc(sizeof(struct rqfpair));
		if (ppair == NULL) {
			log_err(-1, __func__, "rqfpair: malloc failed");
			free_rq_cpyfile_cred(pcf);
			return 0;
		}
		CLEAR_LINK(ppair->fp_link);
		ppair->fp_flag = 0;
		ppair->fp_local = NULL;
		ppair->fp_rmt = NULL;

		if (fgets(buf, sizeof(int), stdin) == NULL) {
			free(ppair);
			ppair = NULL;
			free_rq_cpyfile_cred(pcf);
			log_err(-1, __func__, "Failed to read fp_flag");
			return 0;
		} else {
			buf[strlen(buf) - 1] = '\0';
			ppair->fp_flag = atoi(buf);
		}

		if ((ppair->fp_local = (char *) malloc(MAXPATHLEN + 1)) == NULL) {
			free(ppair);
			ppair = NULL;
			free_rq_cpyfile_cred(pcf);
			log_err(-1, __func__, "Failed to read fp_local");
			return 0;
		} else {
			memset(ppair->fp_local, 0, MAXPATHLEN + 1);
			if (fgets(ppair->fp_local, MAXPATHLEN, stdin) == NULL) {
				free(ppair->fp_local);
				ppair->fp_local = NULL;
				free(ppair);
				ppair = NULL;
				free_rq_cpyfile_cred(pcf);
				return 0;
			} else {
				ppair->fp_local[strlen(ppair->fp_local) - 1] = '\0';
			}
		}

		if ((ppair->fp_rmt = (char *) malloc(MAXPATHLEN + 1)) == NULL) {
			free(ppair->fp_local);
			ppair->fp_local = NULL;
			free(ppair);
			ppair = NULL;
			free_rq_cpyfile_cred(pcf);
			log_err(errno, __func__, "fp_rmt: malloc failed");
			return 0;
		} else {
			memset(ppair->fp_rmt, 0, MAXPATHLEN + 1);
			if (fgets(ppair->fp_rmt, MAXPATHLEN, stdin) == NULL) {
				free(ppair->fp_rmt);
				ppair->fp_rmt = NULL;
				free(ppair->fp_local);
				ppair->fp_local = NULL;
				free(ppair);
				ppair = NULL;
				free_rq_cpyfile_cred(pcf);
				log_err(-1, __func__, "Failed to read fp_rmt");
				return 0;
			} else {
				ppair->fp_rmt[strlen(ppair->fp_rmt) - 1] = '\0';
			}
		}
		append_link(&pcf->rq_pair, &ppair->fp_link, ppair);
	}

	if (fgets(buf, sizeof(buf), stdin) == NULL) {
		free_rq_cpyfile_cred(pcf);
		log_err(-1, __func__, "Failed to read pcf");
		return 0;
	} else {
		buf[strlen(buf) - 1] = '\0';
		if (strcmp(buf, "cred_info") == 0) {
			DWORD a_cnt = 0;

			if (fgets(buf, sizeof(buf), stdin) == NULL) {
				free_rq_cpyfile_cred(pcf);
				log_err(-1, __func__, "Failed to read cred_length");
				return 0;
			} else {
				buf[strlen(buf) - 1] = '\0';
				cred_len = atoi(buf);
			}

			if (fgets(buf, sizeof(buf), stdin) == NULL) {
				free_rq_cpyfile_cred(pcf);
				log_err(-1, __func__, "Failed to read cred_buf");
				return 0;
			} else {
				buf[strlen(buf) - 1] = '\0';
				cred_buf = NULL;
				if (decode_from_base64(buf, &cred_buf, &a_cnt)) {
					sprintf(log_buffer, "Failed to decode buf: %s, len: %d", buf, strlen(buf));
					log_err(-1, __func__, log_buffer);
					return 0;
				}
			}

			if (cred_len != a_cnt) {
				sprintf(log_buffer, "Data mismatch, cred_len: %d, decoded_cred_len: %d", cred_len, a_cnt);
				log_err(-1, __func__, log_buffer);
				free_rq_cpyfile_cred(pcf);
				free(cred_buf);
				return 0;
			}
		} else if (strcmp(buf, "no_cred_info") == 0) {
			cred_buf = NULL;
			cred_len = 0;
		}
	}
	return 1;
}

/**
 * @brief
 *	send_pcphosts - Send cphosts structure (information about usecp from mom config file)
 *	and cphosts_num (no. of usecp entry in cphosts struct) to
 *	given pipe handles <pio>
 *
 * @param[in]	pio      -	pio_handles struct which include
 *				handle to pipe on which info will be sent
 * @param[in]	pcphosts -	pointer to cphosts struct which will be sent to pipe
 *
 * @return	void
 *
 */
void
send_pcphosts(pio_handles *pio, struct cphosts *pcphosts)
{
	char buf[CPY_PIPE_BUFSIZE + 1] = {'\0'};
	int nh = 0;

	if ((cphosts_num <= 0) || (pcphosts == NULL))
		/* nothing to send, just return */
		return;

	snprintf(buf, sizeof(buf) - 1, "%s\n", "pcphosts=");
	check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

	snprintf(buf, sizeof(buf) - 1, "%d\n", cphosts_num);
	check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

	for (nh = 0; nh < cphosts_num; nh++) {
		snprintf(buf, sizeof(buf) - 1, "%s\n", (pcphosts + nh)->cph_hosts);
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

		snprintf(buf, sizeof(buf) - 1, "%s\n", (pcphosts + nh)->cph_from);
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

		snprintf(buf, sizeof(buf) - 1, "%s\n", (pcphosts + nh)->cph_to);
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));
	}
}

/**
 * @brief
 *	send_rq_cpyfile_cred - Send given rq_cpyfile structure and cred info (if any) to
 *	given pipe handles <pio>
 *
 * @param[in]	pio	-	pio_handles struct which include
 *				handle to pipe on which info will be sent
 * @param[in]	pcf	-	pointer to rq_cpyfile struct which will be sent to pipe
 *
 * @return	int
 * @retval	0 - on Error
 * @retval  	1 - on Success
 *
 * @note	This function will cred info from global variable cred_buf and cred_len
 *
 */
int
send_rq_cpyfile_cred(pio_handles *pio, struct rq_cpyfile *pcf)
{
	char buf[CPY_PIPE_BUFSIZE + 1] = {'\0'};
	int pair_ct = 0;
	struct rqfpair *ppair = NULL;

	if ((pio == NULL) || (pcf == NULL)) {
		return 0;
	}

	ppair = (struct rqfpair *) GET_NEXT(pcf->rq_pair);
	while (ppair) {
		++pair_ct;
		ppair = (struct rqfpair *) GET_NEXT(ppair->fp_link);
	}

	snprintf(buf, sizeof(buf) - 1, "%s\n", "rq_cpyfile=");
	check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

	snprintf(buf, sizeof(buf) - 1, "%s\n", pcf->rq_jobid);
	check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

	snprintf(buf, sizeof(buf) - 1, "%s\n", pcf->rq_owner);
	check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

	snprintf(buf, sizeof(buf) - 1, "%s\n", pcf->rq_user);
	check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

	snprintf(buf, sizeof(buf) - 1, "%s\n", pcf->rq_group);
	check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

	snprintf(buf, sizeof(buf) - 1, "%d\n", pcf->rq_dir);
	check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

	snprintf(buf, sizeof(buf) - 1, "%d\n", pair_ct);
	check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

	ppair = (struct rqfpair *) GET_NEXT(pcf->rq_pair);
	while (ppair) {
		snprintf(buf, sizeof(buf) - 1, "%d\n", ppair->fp_flag);
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

		snprintf(buf, sizeof(buf) - 1, "%s\n", ppair->fp_local);
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

		snprintf(buf, sizeof(buf) - 1, "%s\n", ppair->fp_rmt);
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

		ppair = (struct rqfpair *) GET_NEXT(ppair->fp_link);
	}

	if (cred_buf != NULL && cred_len != 0) {
		char *str = NULL;
		snprintf(buf, sizeof(buf) - 1, "cred_info\n");
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

		snprintf(buf, sizeof(buf) - 1, "%d\n", cred_len);
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));

		if (encode_to_base64(cred_buf, cred_len, &str)) {
			sprintf(log_buffer, "Failed to encode cred_buf: %s, len: %d", cred_buf, cred_len);
			log_err(-1, __func__, log_buffer);
			return 0;
		}
		snprintf(buf, sizeof(buf) - 1, "%s\n", str);
		free(str);
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));
	} else {
		snprintf(buf, sizeof(buf) - 1, "no_cred_info\n");
		check_err(__func__, buf, win_pwrite(pio, buf, strlen(buf)));
	}
	return 1;
}
#endif
